package com.arvato.minipro.service.mall;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_do.mall.EcCutDo;
import com.arvato.minipro.bean.bean_do.mall.EcCutItemDo;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_vo.mall.CommodityCutVo;
import com.arvato.minipro.bean.bean_vo.mall.EcCutItemVo;
import com.arvato.minipro.bean.bean_vo.mall.EcCutVo;
import com.arvato.minipro.mapper.mall.EcCutItemMapper;
import com.arvato.minipro.mapper.mall.EcCutMapper;

/**
 * 
 * @author: smallking
 * 
 * @Descibe:处理砍价的Service
 * 
 * @Date: 2018年7月6日  下午1:34:40
 */
@Service
@Transactional
public class CutDownService {
	
	@Autowired
	private CommodityCutService commodityCutService;
	
	@Autowired
	private EcCutMapper cutMapper;
	
	@Autowired
	private EcCutItemMapper cutItemMapper;
	
	/**
	 * 根据ID查询一个用户的砍价
	 * @param cutId
	 * @return
	 */
	public EcCutVo getCutVoById(String cutId) {
		EcCutVo cutVo = cutMapper.getCutById(cutId);
		setCutVo(cutVo);
		return cutVo;
	}
	
	/**
	 * 根据条件查询一个用户的砍价
	 * @param query
	 * @return
	 */
	public EcCutVo getCutByQuery(Query query) {
		List<EcCutVo> list = cutMapper.listCutByQuery(query);
		if(list.size() >= 1) {
			EcCutVo cutVo = list.get(0);
			setCutVo(cutVo);
			return cutVo;
		}
		return null;
	}
	/**
	 * 新增一个用户砍价
	 * @param cutDo
	 */
	public int insert(EcCutDo cutDo) {
		CommodityCutVo commodityCutVo = commodityCutService.getCommodityCutById(cutDo.getCommodityCutId());
		// 将价格化为分为单位  通过整数进行计算
		int oriPrice = (int)(commodityCutVo.getCommodityVo().getPrice() * 100);
		int floorPrice = (int)(commodityCutVo.getFloorPrice() * 100);
		int upperLimit = (int)(commodityCutVo.getUpperLimit() * 100);
		int lowerLimit = (int)(commodityCutVo.getLowerLimit() * 100);
		// 根据可以砍价的总金额 砍价次数 单人上限，单人下限 生成砍价金额拼接成的数组
		String priceListStr = "";
		try {
			priceListStr = divide(commodityCutVo.getCutCount(), oriPrice - floorPrice, upperLimit, lowerLimit);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(priceListStr .equals("")) {
			return 0;
		}
		cutDo.setPriceListStr(priceListStr);
		return cutMapper.insert(cutDo);
	}
	/**
	 * 修改一个用户砍价
	 * @param cutDo
	 * @return
	 */
	public int update(EcCutDo cutDo) {
		return cutMapper.update(cutDo);
	}
	
	/**
	 * 新增用户砍价详情
	 * @param cutItemDo
	 * @return
	 */
	public int insert(EcCutItemDo cutItemDo) {
		return cutItemMapper.insert(cutItemDo);
	}

	/**
	 * 设置cutVo的一些信息详情
	 * @param cutVo
	 */
	private void setCutVo(EcCutVo cutVo) {
		Query query = new Query();
		query.put("cutId", cutVo.getCutId());
		List<EcCutItemVo> cutItemList = cutItemMapper.listCutItemByQuery(query);
		cutVo.setCutItemList(cutItemList);
		CommodityCutVo commodityCutVo = commodityCutService.getCommodityCutById(cutVo.getCommodityCutId());
		cutVo.setCommodityCutVo(commodityCutVo);
	}
	
	/**
	 * 这个砍价金额生成算法写的有点垃圾了，回头用导数算一下，可以算出入参所要符合的规则，
	 * 例如：upperLimit必须小于 total 等。
	 * @param cutCount
	 * @param total
	 * @param upperLimit
	 * @param lowerLimit
	 * @return
	 */
	private String divide(int cutCount, int total, int upperLimit, int lowerLimit) {
		int total1 = total;
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < cutCount - 1; i++) {
			int item = (int)Math.ceil(lowerLimit + Math.random() * (upperLimit - lowerLimit));
			System.out.println(item);
			list.add(item);
			total -= item;
		}
		list.add(total);
		if (total > upperLimit || total < lowerLimit) {
			return divide(cutCount, total1, upperLimit, lowerLimit);
		}
		String priceListStr = "";
		for(int i = 0; i < list.size(); i ++) {
			double price = (double)list.get(i);
			price = price / 100;
			priceListStr += price;
			if(i != list.size() - 1) {
				priceListStr += ",";
			}
		}
		return priceListStr;
	}

	
	
}
